package cs.xwi7xwa.activities;

import java.io.File;
import java.util.List;

import android.content.res.Resources;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.widget.ImageView;
import cs.xwi7xwa.R;
import cs.xwi7xwa.data.FactDbAdapter;
import cs.xwi7xwa.templates.BumpTemplate;

public class ShareView extends BumpTemplate
{
    private GestureLibrary gLibrary;
    private static final File gestureFile = new File(Environment.getExternalStorageDirectory(), "gestures");
    private Gesture gesture;
    private int id = -1;
    
    private int bmpSize;
    private int bmpInset;
    private int bmpColor;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.share);
        
        // Get all extra data that came with the Intent
        Bundle extras = getIntent().getExtras();
        
        // Populate the page with the extra data
        if (extras != null)
        {
            // If the id does not exist, then use -1 as default
            id = extras.getInt(FactDbAdapter.KEY_ID, -1);
        }
        
        // Load Gesture Library
        gLibrary = GestureLibraries.fromFile(gestureFile);
        if (!gLibrary.load())
        {
            // If it has failed to load library, exit
            finish();
        }
        
        // Load the gesture for this fact
        List<Gesture> gestures = gLibrary.getGestures(Integer.toString(id));
        if (gestures.size() != 1)
        {
            // If there are more than one gesture with this name,
            // something went wrong. Exit.
            finish();
        }
        // Since there is only one gesture, get it from the list
        gesture = gestures.get(0);
        
        // Load numbers for bitmap
        final Resources resources = getResources();
        bmpColor = resources.getColor(R.color.gesture_color);
        bmpInset = (int) resources.getDimension(R.dimen.gesture_thumbnail_inset);
        bmpSize = (int) resources.getDimension(R.dimen.gesture_full_size);
        
        // Generate a bitmap version of the gesture and display the bitmap
        Bitmap bitmap = gesture.toBitmap(bmpSize, bmpSize, bmpInset, bmpColor);
        ImageView imageView = (ImageView) findViewById(R.id.shareGesture);
        imageView.setImageBitmap(bitmap);
    }
}
